#include <header.h>

int main(){

    int socket_fd = socket(AF_INET, SOCK_STREAM, 0);

    struct sockaddr_in sockaddr;
    sockaddr.sin_family = AF_INET;
    sockaddr.sin_port = htons(atoi("8080"));
    sockaddr.sin_addr.s_addr = inet_addr("192.168.106.130");
    bind(socket_fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
    listen(socket_fd, 10);
    int net_fd = accept(socket_fd, NULL, NULL);


    // 等待客户端发送文件名,  文件内容过来 -> 接收 -> 创建文件 -> 写内容 

    char file_name[60] = {0};
    char buf[100] = {0};

    int file_name_len =0;
    recv(net_fd, &file_name_len, sizeof(int), 0);
    recv(net_fd, file_name, file_name_len, 0);
    printf("file_name: %s \n", file_name);

    int size=0;
    recv(net_fd, &size, sizeof(size), 0);
    recv(net_fd, buf, size, 0);

    printf("buf: %s \n", buf);


    int file_fd=open(file_name, O_RDWR|O_CREAT, 0600);

    write(file_fd, buf, size);

    close(file_fd);
    close(net_fd);
    close(socket_fd);
    return 0;
}

